const path = require('path');
const fs = require('fs');

/** 默认参数配置 */
const DEFAULT_CONFIG = {
	isRename: true,
	isVersion: true,
	isMainJs: true,
	isStyleCss: true,
	isCocos2dJs: true,
	isSplashPng: false,

	isSrcSettingsJs: true,
	isSrcAssets: false,

	isZip: false,
};

/** 配置文件路径 */
const CONFIG_PATH = path.join(__dirname, '../config.json');

function getConfig() {
	const configJson = JSON.parse(JSON.stringify(DEFAULT_CONFIG));
	if (fs.existsSync(CONFIG_PATH)) {
		const localConfig = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
		for (const key in configJson) {
			if (localConfig[key] !== undefined) {
				configJson[key] = localConfig[key];
			}
		}
	}
	return configJson;
}

function setConfig(value) {
	const configJson = JSON.parse(JSON.stringify(DEFAULT_CONFIG));
	for (const key in configJson) {
		if (value[key] !== undefined) {
			configJson[key] = value[key];
		}
	}
	fs.writeFileSync(CONFIG_PATH, JSON.stringify(configJson, null, 2));
}

// panel/index.js, this filename needs to match the one registered in package.json
Editor.Panel.extend({
	// css style for panel
	style: `
    :host { margin: 5px; }
    h2 { color: #f90; }
  `,

	// html template for panel
	template: `
	<h2>构建后自动化操作</h2>
	
	<hr />
	<ui-checkbox id="isRename" >1. 是否修改 web-mobile 为项目名（构建完成后触发）</ui-checkbox>
	<hr />
	<ui-checkbox id="isVersion" >2. 是否更新版本号</ui-checkbox>
	<hr />
	<ui-checkbox id="isMainJs" >3. 是否合并 main.js</ui-checkbox>
	<hr />
	<ui-checkbox id="isStyleCss" >4. 是否合并 style-mobile.css、style-desktop.css</ui-checkbox>
	<hr />
	<ui-checkbox id="isCocos2dJs" >5. 是否删除 cocos2d-js.js</ui-checkbox>
	<hr />
	<ui-checkbox id="isSplashPng" >6. 是否删除 splash.png</ui-checkbox>
	<hr />
	<ui-checkbox id="isSrcSettingsJs" >7. 是否合并 src/settings.js（构建完成后触发）</ui-checkbox>
	<hr />
	<ui-checkbox id="isSrcAssets" >8. 是否合并 src/ 内其他文件（构建完成后触发）</ui-checkbox>
	<hr />
	<ui-checkbox id="isZip" >9. 是否压缩 Bundle 包成 Zip（构建完成后触发）</ui-checkbox>

	<hr />
	<ui-button id="btn">保存</ui-button>
  `,

	// element and variable binding
	$: {
		btn: '#btn',

		isRename: '#isRename',
		isVersion: '#isVersion',
		isMainJs: '#isMainJs',
		isStyleCss: '#isStyleCss',
		isCocos2dJs: '#isCocos2dJs',
		isSplashPng: '#isSplashPng',

		isSrcSettingsJs: '#isSrcSettingsJs',
		isSrcAssets: '#isSrcAssets',

		isZip: '#isZip',
	},

	// method executed when template and styles are successfully loaded and initialized
	ready() {
		this.$btn.addEventListener('confirm', () => {
			Editor.Ipc.sendToMain('ccc-build-complete:saveData');
		});
		Editor.Ipc.sendToPanel('ccc-build-complete', 'ccc-build-complete:initData');
	},

	// register your ipc messages here
	messages: {
		'ccc-build-complete:initData'(event) {
			let info = getConfig();
			this.$isRename.checked = info.isRename;
			this.$isVersion.checked = info.isVersion;
			this.$isMainJs.checked = info.isMainJs;
			this.$isStyleCss.checked = info.isStyleCss;
			this.$isCocos2dJs.checked = info.isCocos2dJs;
			this.$isSplashPng.checked = info.isSplashPng;

			this.$isSrcSettingsJs.checked = info.isSrcSettingsJs;
			this.$isSrcAssets.checked = info.isSrcAssets;

			this.$isZip.checked = info.isZip;
		},

		'ccc-build-complete:saveData'(event) {
			let info = {
				isRename: this.$isRename.checked,
				isVersion: this.$isVersion.checked,
				isMainJs: this.$isMainJs.checked,
				isStyleCss: this.$isStyleCss.checked,
				isCocos2dJs: this.$isCocos2dJs.checked,
				isSplashPng: this.$isSplashPng.checked,

				isSrcSettingsJs: this.$isSrcSettingsJs.checked,
				isSrcAssets: this.$isSrcAssets.checked,

				isZip: this.$isZip.checked,
			};

			setConfig(info);
			return info;
		},
	},
});
